<?php


namespace App\Api;
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS,PATCH');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

use App\Domain\Goods as GoodsDomain;
use PhalApi\Api;

class Goods extends Api
{
    public function getRules()
    {
        return array(
            'getGoodInfo' => array(
                'good_id' => array('name' => 'good_id', 'require' => true, 'min' => 1, 'desc' => '商品的id')
            ),
            'getGoodInfoByTypeName' => array(
                'good_type' => array('name' => 'good_type', 'require' => true, 'min' => 1, 'desc' => '商品类型的名称')
            ),
            'insertGoodsInfo' => array(
                'good_name' => array('name' => 'good_name', 'require' => true, 'min' => 1, 'max' => 50, 'source' => 'post', 'desc' => '商品的名称'),
                'good_price' => array('name' => 'good_price', 'require' => true, 'min' => 1, 'source' => 'post', 'desc' => '商品的价格'),
                'good_typeName' => array('name' => 'good_typeName', 'require' => true, 'min' => 1, 'max' => 20, 'source' => 'post', 'desc' => '商品的类型'),
                'good_star' => array('name' => 'good_star', 'require' => true, 'min' => 1, 'source' => 'post', 'desc' => '商品的评分'),
                'good_pubdate' => array('name' => 'good_pubdate', 'require' => true, 'min' => 1, 'source' => 'post', 'desc' => '商品的添加时间'),
                'good_picture' => array('name' => 'good_picture', 'require' => true, 'min' => 1, 'max' => 150, 'source' => 'post', 'desc' => '商品的图片路径'),
                'good_intro' => array('name' => 'good_intro', 'require' => true, 'min' => 1, 'max' => 200, 'source' => 'post', 'desc' => '商品的简介'),

            ),
            'updateGoodsInfo' => array(
                'good_id' => array('name' => 'good_id', 'require' => true, 'min' => 1, 'source' => 'post', 'desc' => '商品的id'),
                'good_name' => array('name' => 'good_name', 'require' => true, 'min' => 1, 'max' => 50, 'source' => 'post', 'desc' => '商品的名称'),
                'good_price' => array('name' => 'good_price', 'require' => true, 'min' => 1, 'source' => 'post', 'desc' => '商品的价格'),
                'good_typeName' => array('name' => 'good_typeName', 'require' => true, 'min' => 1, 'max' => 20, 'source' => 'post', 'desc' => '商品的类型'),
                'good_pubdate' => array('name' => 'good_pubdate', 'require' => true, 'min' => 1, 'source' => 'post', 'desc' => '商品的添加时间'),
                'good_picture' => array('name' => 'good_picture', 'require' => true, 'min' => 1, 'max' => 150, 'source' => 'post', 'desc' => '商品的图片路径'),
                'good_intro' => array('name' => 'good_intro', 'require' => true, 'min' => 1, 'max' => 200, 'source' => 'post', 'desc' => '商品的简介'),
            ),
            'deleteGoodsInfo' => array(
                'good_id' => array('name' => 'good_id', 'require' => true, 'min' => 1, 'source' => 'post', 'desc' => '商品的id'),
            ),
            'likeQueryByGoodsName' => array(
                'like_goodName' => array('name' => 'like_goodName', 'require' => true, 'min' => 1, 'desc' => '模糊的商品名称')
            ),
            'likeQueryByGoodsDate' => array(
                'like_goodDate' => array('name' => 'like_goodDate', 'require' => true, 'min' => 1, 'desc' => '模糊的商品名称')
            ),

        );
    }

    /**
     * 返回所有商品列表
     * @desc 获取所有商品列表
     * return @array 返回所有商品信息的json
     */
    public function getGoodsInfo()
    {
        $domain = new GoodsDomain();
        $info = $domain->getGoodsInfo();
        return array('info' => $info);
    }

    /**
     * 返回指定id的商品详细信息
     * @desc 获取指定商品信息的列表
     * return @array 返回指定商品信息的json
     */
    public function getGoodInfo()
    {
        $good_id = $_GET['good_id'];//带参数传输方法
        $domain = new GoodsDomain();
        $info = $domain->getGoodInfo($good_id);
        return array('info' => $info);
    }

    /**
     * 返回指定商品typename的信息列表
     * @desc 获取指定商品类型名称的所有商品信息
     * return @array 返回按照指定商品类型名称搜索返回的json数组
     */
    public function getGoodInfoByTypeName()
    {
        $good_type = $_GET['good_type'];
        $domain = new GoodsDomain();
        $info = $domain->getGoodInfoByTypeName($good_type);
        return array('info' => $info);
    }

    /**
     * 增加商品的信息
     * @desc 通过商品名称、商品价格、商品类型、商品热度、商品创建日期、商品图片链接、商品简介七个参数插入商品信息
     * 返回新增的ID
     */
    public function insertGoodsInfo()
    {
        $good_name = $_POST['good_name'];
        $good_price = $_POST['good_price'];
        $good_typeName = $_POST['good_typeName'];
        $good_star = $_POST['good_star'];
        $good_pubdate = $_POST['good_pubdate'];
        $good_picture = $_POST['good_picture'];
        $good_intro = $_POST['good_intro'];
        $domain = new GoodsDomain();
        $id = $domain->insertGoodsInfo($good_name, $good_price, $good_typeName, $good_star, $good_pubdate, $good_picture, $good_intro);
        return array('id' => $id);
    }


    /**
     * 更新商品的信息
     * @desc 通过商品名称、商品价格、商品类型、商品创建日期、商品图片链接、商品简介，商品id七个参数更新商品类型，前六个是要设置的值
     * 更新异常时返回false，数据无变化时返回0，成功更新返回影响的行数
     */
    public function updateGoodsInfo()
    {
        $good_id = $_POST['good_id'];
        $good_name = $_POST['good_name'];
        $good_price = $_POST['good_price'];
        $good_typeName = $_POST['good_typeName'];
        $good_pubdate = $_POST['good_pubdate'];
        $good_picture = $_POST['good_picture'];
        $good_intro = $_POST['good_intro'];
        $domain = new GoodsDomain();
        $info = $domain->updateGoodsInfo($good_id, $good_name, $good_price, $good_typeName, $good_pubdate, $good_picture, $good_intro);
        return array('info' => $info);
    }

    /**
     * 删除商品的信息
     * @desc 通过商品的id进行删除
     * 按id进行删除，并返回影响的行数
     */
    public function deleteGoodsInfo()
    {
        $good_id = $_POST['good_id'];
        $domain = new GoodsDomain();
        $row = $domain->deleteGoodsInfo($good_id);
        return array('row' => $row);
    }

    /**
     * 按名称模糊查询商品
     * @desc 传商品名称的相关信息，查询相匹配的商品，实现模糊查询
     */
    public function likeQueryByGoodsName()
    {
        $like_goodName = $_GET['like_goodName'];
        $domain = new GoodsDomain();
        $info = $domain->likeQueryByGoodsName($like_goodName);
        return array('info' => $info);
    }

    /**
     * 按名称模糊查询商品
     * @desc 传商品名称的相关信息，查询相匹配的商品，实现模糊查询
     */
    public function likeQueryByGoodsDate()
    {
        $like_goodDate = $_GET['like_goodDate'];
        $domain = new GoodsDomain();
        $info = $domain->likeQueryByGoodsDate($like_goodDate);
        return array('info' => $info);
    }

}